﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace MiniBlog
{
    public class MiniBlog : IMiniBlog
    {
        public IEnumerable<PostDTO> GetPosts(string numberOfPostsInString, string numberOfSymbolsInString)
        {
            int numberOfPosts = 0;
            int numberOfSymbols = 0;
            int.TryParse(numberOfPostsInString, out numberOfPosts);
            int.TryParse(numberOfSymbolsInString, out numberOfSymbols);

            IEnumerable<PostDTO> posts;
            IEnumerable<PostDTO> postsDTO;

            MiniBlogEntities context = new MiniBlogEntities();
            posts = from p in context.Posts
                    select new PostDTO
                    {
                        PostId = p.PostId,
                        Title = p.Title,
                        Author = p.Autor,
                        Body = p.Body.Substring(0, numberOfSymbols),
                        Date = p.Date,
                        CommentsCount = p.Comments.Count
                    };


            postsDTO = posts.Take(numberOfPosts);
            return postsDTO;
        }

        public void PostMessage(CommentPost commentPost)
        {
            Comment comment = new Comment();
            comment.PostId = commentPost.PostId;
            comment.Author = commentPost.Author;
            comment.Body = commentPost.Body;
            comment.Date = DateTime.Now;

            MiniBlogEntities context = new MiniBlogEntities();
            context.Comments.AddObject(comment);
            context.SaveChanges();
        }

        //public void PostMessage()
        //{
        //}

        public string Test()
        {
            return "test";
        }
    }
}
